﻿Imports System.IO

Public Class SubForm_Magic_Numers
    'Variabili Globali-----------------------------------------------------------------------------------------------------
    Public Shared magic_num() As String = (Split(Trim(My.Resources.Magic_Numbers), Chr(13))) 'Array contenente i magic Numbers
    Public Shared CanClose As Byte = 0

    Private Sub SubForm_Magic_Numers_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If CanClose = 0 Then
            e.Cancel = True
        End If
    End Sub

    Private Sub SubForm_Magic_Numers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = New Point(MainForm.Location.X - 260, MainForm.Location.Y)
        Dim riga_MN As String = ""
        Dim pos As Short = 0
        'Me.Right = MainForm.Location.Y - 200
        If File.Exists(Application.StartupPath & "\Magic_Numbers.txt") Then
            Dim op_io As New StreamReader(Application.StartupPath & "\Magic_Numbers.txt")
            Do Until op_io.EndOfStream
                riga_MN = op_io.ReadLine
                If riga_MN <> "" Then
                    ReDim Preserve magic_num(pos)
                    magic_num(pos) = riga_MN
                    pos += 1
                    List_Magic_num.Items.Add(riga_MN)
                End If
            Loop
            riga_MN = Nothing
        Else
            Dim sr As New StringReader(My.Resources.Magic_Numbers)
            riga_MN = sr.ReadLine
            Do Until riga_MN = Nothing
                ReDim Preserve magic_num(pos)
                magic_num(pos) = riga_MN
                pos += 1
                List_Magic_num.Items.Add(riga_MN)
                riga_MN = sr.ReadLine
            Loop
        End If
        CanClose = 0
    End Sub

    Private Sub txt_Cerca_MN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_Cerca_MN.TextChanged
        Try
            If txt_Cerca_MN.TextLength = 3 Then
                For pos As Short = 0 To SubForm_Magic_Numers.magic_num.Length
                    If magic_num(pos).Substring(0, 3) = txt_Cerca_MN.Text Then
                        List_Magic_num.SetSelected(pos, True)
                    ElseIf magic_num(pos).Substring(0, 3) > txt_Cerca_MN.Text And magic_num(pos + 1).Substring(0, 3) > txt_Cerca_MN.Text Then
                        List_Magic_num.SetSelected(pos + 2, True)
                    End If
                Next
                txt_Cerca_MN.Text = ""
            ElseIf txt_Cerca_MN.TextLength > 3 Then
                txt_Cerca_MN.Text = ""
            End If
        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            ProjectData.ClearProjectError()
        End Try
    End Sub

    Private Sub SubForm_Magic_Numers_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
        Me.Focus()
        If Me.Focused = True Then
            MainForm.Location = New Point(Me.Left + 260, Me.Location.Y)
        End If
    End Sub

    Private Sub Cmd_CAL_MN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmd_CAL_MN.Click
        Try
            Dim SSID As Double = txt_ssid2.Text
            Dim SN2 As Integer = txt_seriale2.Text.Substring(6, 7)
            Dim SN1 As String = txt_seriale2.Text.Substring(0, 5)
            Dim MAC As String = ""
            For pos As Short = 0 To SubForm_Magic_Numers.magic_num.GetUpperBound(0)
                If magic_num(pos).Substring(0, 3) = txt_ssid2.Text.Substring(0, 3) Then
                    MAC = magic_num(pos).Substring(22, 6)
                    Exit For
                ElseIf magic_num(pos).Substring(0, 3) > txt_ssid2.Text.Substring(0, 3) And magic_num(pos + 1).Substring(0, 3) < txt_ssid2.Text.Substring(0, 3) Then
                    MAC = magic_num(pos + 1).Substring(22, 6)
                    Exit For
                End If
            Next

            txt_MN.Text = ""
            For K As Byte = 8 To 14 Step 5
                Dim Q As Long = CLng(Math.Round(CDbl(SSID - SN2 * K)))
                If K = 8 Then
                    txt_MN.Text = CStr(SSID).Substring(0, 3) & "," & SN1 & "," & "0" & K & "," & Q & "," & MAC
                ElseIf K = 13 Then
                    txt_MN.AppendText(Chr(13) & CStr(SSID).Substring(0, 3) & "," & SN1 & "," & K & "," & Q & "," & MAC)
                End If
            Next

        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            ProjectData.ClearProjectError()
        End Try
    End Sub

    Private Sub Cmd_Crea_ListaMN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmd_Crea_ListaMN.Click
        Try
            System.IO.File.WriteAllLines(Application.StartupPath & "\Magic_Numbers.txt", magic_num)
            MsgBox("Creazione del file Magic_Numbers.txt completata")

        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            ProjectData.ClearProjectError()
        End Try
    End Sub
End Class